package org.info.stock.excel;

import com.alibaba.fastjson.JSONObject;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.info.stock.entity.Person;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ReadExcel {
    public static void x(String[] args) throws Exception {
        long t1 = new Date().getTime();
        excel();
        long t2 = new Date().getTime();

        System.out.println((t2-t1)/1000 + "秒");
    }
    public static void excel() throws Exception {
        File file = new File("D:\\yaolaobaoxian\\养老保险-12.xls");
        if (!file.exists()){
            throw new Exception("文件不存在!");
        }
        InputStream in = new FileInputStream(file);

        // 读取整个Excel
        HSSFWorkbook sheets = new HSSFWorkbook(in);
        // 获取第一个表单Sheet
        HSSFSheet sheetAt = sheets.getSheetAt(0);

        //默认第一行为标题行，i = 0
        HSSFRow titleRow = sheetAt.getRow(0);
        // 循环获取每一行数据
        List<Person> personList = new ArrayList<>();
        for (int i = 0; i < 740; i++) {
            HSSFRow row = sheetAt.getRow(i + 1);
            Person person = new Person();
            if (row != null){
                if (!row.getCell(1).toString().equals(""))
                    person.setName(row.getCell(1).toString());
                if (!row.getCell(2).toString().equals(""))
                    person.setIdcard(row.getCell(2).toString());
                personList.add(person);
            }
        }
        personList.forEach(item->{
            // 请求头
            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Type", "application/json");
            headers.add("Accept", "application/json, text/javascript, */*; q=0.01");
            // 请求
            JSONObject requestMap = new JSONObject();
            requestMap.put("cardid", item.getIdcard());
            requestMap.put("name", item.getName());

            HttpEntity<JSONObject> entity = new HttpEntity<>(requestMap, headers);
            RestTemplate restTemplate = new RestTemplate();
            String url ="https://qydzh.zwfw.guizhou.gov.cn/not-check/api/sjzb/gwyfpkfldxz/interface/pkryxxcx";
            JSONObject body1 = restTemplate.postForObject(url, entity, JSONObject.class);
            String response = body1.getString("response");
            item.setResponse(response);
        });
        System.out.println("接口返回参数:" + personList);

    }
}
